package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.eneity.BaseAttrInfo;
import com.atguigu.gmall.product.eneity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台属性
 */
@Api(tags = "平台属性")
@RestController
@RequestMapping("/admin/product")
public class BaseAttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    //attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @ApiOperation("根据分类id获取平台属性")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getBaseAttr(@PathVariable Long category1Id,
                            @PathVariable Long category2Id,
                              @PathVariable Long category3Id
    ){
        List<BaseAttrInfo> list = baseAttrInfoService.getBaseAttrAndValue(category1Id,category2Id,category3Id);
        return Result.ok(list);
    }

    @ApiOperation("添加/修改平台属性")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){

        if (baseAttrInfo.getId() == null){
            baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        }else {
            baseAttrInfoService.updateAttrInfo(baseAttrInfo);
        }

        return Result.ok();
    }

    @ApiOperation("根据平台属性ID获取平台属性对象数据")
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrInfoValue(@PathVariable Long attrId){

        List<BaseAttrValue> list = baseAttrValueService.getAttrInfoValue(attrId);
        return Result.ok(list);
    }

}
